package com.ruoyi.kjcg.jxdj.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.kjcg.jxdj.domain.Jxdj;
import com.ruoyi.kjcg.jxdj.domain.JxdjFj;
import com.ruoyi.kjcg.jxdj.service.IJxdjService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.List;

/**
 * 奖项登记Controller
 *
 * @author huacai
 * @date 2024-11-07
 */
@RestController
@RequestMapping("/kjcg/jxdj")
public class JxdjController extends BaseController {
    @Resource
    private IJxdjService jxdjService;

    /**
     * 查询奖项登记列表
     */
    @PreAuthorize("@ss.hasPermi('kjcg:jxdj:list')")
    @GetMapping("/list")
    public TableDataInfo list(Jxdj jxdj) {
        startPage();
        List<Jxdj> list = jxdjService.selectJxdjList(jxdj);
        return getDataTable(list);
    }

    /**
     * 导出奖项登记列表
     */
    @PreAuthorize("@ss.hasPermi('kjcg:jxdj:export')")
    @Log(title = "奖项登记", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Jxdj jxdj) {
        List<Jxdj> list = jxdjService.selectJxdjList(jxdj);
        ExcelUtil<Jxdj> util = new ExcelUtil<Jxdj>(Jxdj.class);
        util.exportExcel(response, list, "奖项登记数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<Jxdj> util = new ExcelUtil<Jxdj>(Jxdj.class);
        util.importTemplateExcel(response, "奖项登记数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "奖项登记", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('kjcg:jxdj:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<Jxdj> util = new ExcelUtil<Jxdj>(Jxdj.class);
        InputStream inputStream = file.getInputStream();
        List<Jxdj> list = util.importExcel(inputStream);
        inputStream.close();
        int count = jxdjService.batchInsertJxdj(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取奖项登记详细信息
     */
    @PreAuthorize("@ss.hasPermi('kjcg:jxdj:query')")
    @GetMapping(value = "/{jxdjId}")
    public AjaxResult getInfo(@PathVariable("jxdjId") String jxdjId) {
        return success(jxdjService.selectJxdjByJxdjId(jxdjId));
    }

    /**
     * 新增奖项登记
     */
    @PreAuthorize("@ss.hasPermi('kjcg:jxdj:add')")
    @Log(title = "奖项登记", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Jxdj jxdj) {
        jxdj.setUserId(Math.toIntExact(getUserId())); //用户ID
        jxdj.setDeptId(Math.toIntExact(getDeptId())); //部门ID
        jxdj.setCreateBy(getUsername()); //创建人
        jxdj.setIdWhere("存在");
        return toAjax(jxdjService.insertJxdj(jxdj));
    }

    /**
     * 修改奖项登记
     */
    @PreAuthorize("@ss.hasPermi('kjcg:jxdj:edit')")
    @Log(title = "奖项登记", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Jxdj jxdj) {
        return toAjax(jxdjService.updateJxdj(jxdj));
    }

    /**
     * 删除奖项登记
     */
    @PreAuthorize("@ss.hasPermi('kjcg:jxdj:remove')")
    @Log(title = "奖项登记", businessType = BusinessType.DELETE)
    @DeleteMapping("/{jxdjIds}")
    public AjaxResult remove(@PathVariable String[] jxdjIds) {
        return toAjax(jxdjService.deleteJxdjByJxdjIds(jxdjIds));
    }

    /**
     * 获得奖项登记主键ID (UUID)
     */
    @GetMapping("/getJxDjId")
    public AjaxResult getJxDjId() {
        String getJxDjId = IdUtils.fastSimpleUUID();
        return success(getJxDjId);
    }

    /**
     * 新增附件
     */
    @PostMapping("/add/File/By/subId")
    public AjaxResult insertJxdjFjByJxdjId(@RequestBody JxdjFj jxdjFj) {
        return toAjax(jxdjService.insertJxdjFjByJxdjId(jxdjFj));
    }

    /**
     * 根据ID查询附件列表
     */
    @GetMapping("/get/FileList/By/subId")
    public AjaxResult selectJxdjFjList(String sId) {
        JxdjFj jxdjFj = new JxdjFj();
        jxdjFj.setJxdjId(sId);
        return AjaxResult.success(jxdjService.selectJxdjFjList(jxdjFj));
    }

    /**
     * 删除奖项登记附件
     */
    @Log(title = "奖项登记附件", businessType = BusinessType.DELETE)
    @DeleteMapping("/fj/{fjIds}")
    public AjaxResult deleteJxdjFjByFjIds(@PathVariable Integer[] fjIds) {
        return toAjax(jxdjService.deleteJxdjFjByFjIds(fjIds));
    }

}
